﻿using AntMovement.Core.Enum;
using AntMovement.Core;
using AntMovement.ViewModels.Dialog;
using AntMovement.Views.Dialog;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntMovement.WPF.Tool;

namespace AntMovement.Domain
{
    public class DialogStarryEdge : IDialogHost
    {
        private Func<string, LogLevel, bool, PopUpType, DialogShowButton, Task<object>> _messageDialogs = null;

        public DialogStarryEdge()
        {
            _messageDialogs = new Func<string, LogLevel, bool, PopUpType, DialogShowButton, Task<object>>(async (str, levelType, isOper, host, showButton) =>
            {
                IsOpenDialog(host);
                string title = "信 息";
                switch (levelType)
                {
                    case LogLevel.Error:
                        title = "错 误";
                        break;
                    case LogLevel.Info:
                        title = "信 息";
                        break;
                    case LogLevel.Warning:
                        title = "警 告";
                        break;
                    case LogLevel.Success:
                        title = "成 功";
                        break;
                }
                return await DialogHost.Show(new MessagePopup()
                {
                    DataContext = new MessagePopupDialog(title, str, showButton, levelType)
                }, host.ToString());
            });

        }

        public Task<object> DialogShow(string message, LogLevel logLevel, bool isOperation = false, PopUpType hostDialog = PopUpType.UserWindow, DialogShowButton dialogShowButton = DialogShowButton.None)
        {
            return ThreadHelper.IsUIThread(() => _messageDialogs.Invoke(message, logLevel, isOperation, hostDialog, dialogShowButton),
                () => ThreadHelper.CrossThreadAsync(sender => _messageDialogs.Invoke(message, logLevel, isOperation, hostDialog, dialogShowButton), (object)1));
        }


        private void IsOpenDialog(PopUpType hostDialog)
        {
            if (DialogHost.IsDialogOpen(hostDialog.ToString())) DialogHost.Close(hostDialog.ToString());
        }
    }
}
